<?php

namespace app\user\controller;

use app\common\respond\HttpApi;
use app\user\logic\AuthLogic;

class LoginController
{
    /**
     * @Description 登录
     * @author: zj
     * @param HttpApi $respond
     * @time 2023/08/29 18:20:49
     * @return null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function login(HttpApi $respond)
    {
        $param = [
            'phone' => request()->post('phone'),
            'password' => request()->post('password')
        ];

        $rule = [
            'phone' => 'require|mobile',
            'password' => 'require|max:30'
        ];
        validate($rule)->check($param);

        $loginLogic = new AuthLogic();
        $data = $loginLogic->login($param['phone'],$param['password']);

        return $respond->success($data['data']);
    }

    /**
     * @Description 退出登录
     * @author: zj
     * @time 2023/09/09 17:37:12
     * @return null
     */
    public function logout()
    {
        $token = request()->header('token');
        $userId = request()->user_info['user_id'];
        $loginLogic = new AuthLogic();
        $loginLogic->logout($token,$userId);

        $respond = new HttpApi();

        return $respond->success([]);
    }
}